package com.qf.agro.utils;

import com.qf.agro.common.SysUserHolder;
import com.qf.agro.mapper.SysLogMapper;
import com.qf.agro.pojo.SysLog;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysLogService;
import org.apache.commons.collections4.Get;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.utils
 * @Date 2023/7/4 20:30
 */
@Aspect
@Component
public class LogAspect {
    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Around("execution(* com.qf.agro.controller.*.*(..))")
    public Object logAroundAdvice(ProceedingJoinPoint pjp){
        Object rtValue=null;
        try {
            SysUser sysUser = SysUserHolder.get();
            SysLog sysLog=new SysLog();
            Object[] args = pjp.getArgs();
            rtValue=pjp.proceed(args);
            if(sysUser!=null){
                sysLog.setUsername(sysUser.getUsername());
                sysLog.setCreateTime(new Date());
                sysLog.setCompanyId(sysUser.getCompanyId());
                sysLog.setCompanyName(sysUser.getCompanyName());
                sysLog.setIp(httpServletRequest.getRemoteAddr());
                //获取签名
                Signature signature = pjp.getSignature();
                MethodSignature methodSignature= (MethodSignature) signature;
                Method method = methodSignature.getMethod();
                String name = method.getName();
                Class<?> declaringClass = method.getDeclaringClass();
                sysLog.setMethod(name);
                String action=getAction(method);
                sysLog.setAction(action);
            }
            sysLogMapper.insert(sysLog);


        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return rtValue;
    }

    private String getAction(Method method) {

       if(method.isAnnotationPresent(GetMapping.class)){
           GetMapping getMapping = method.getAnnotation(GetMapping.class);
           return getMapping.name();
       }
        if(method.isAnnotationPresent(PutMapping.class)){
            PutMapping putMapping = method.getAnnotation(PutMapping.class);
            return putMapping.name();
        }
        if(method.isAnnotationPresent(DeleteMapping.class)){
            DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
            return deleteMapping.name();
        }
        if(method.isAnnotationPresent(PostMapping.class)){
            PostMapping postMapping = method.getAnnotation(PostMapping.class);
            return postMapping.name();
        }
       return null;
    }

}
